//
//  AppDelegate.m
//  McRemote
//
//  Created by Magic on 12/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+InitConfig.h"
#import "MRClient.h"
#import "MRMessageCenter.h"

@implementation AppDelegate

@synthesize window = _window;


@synthesize tabBarController = _tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self firstTimeUsageConfig];
    
    [[MRClient sharedClient] searchForServicesOfType:@"mcremote"];
    [[MRClient sharedClient] setDelegate:[MRMessageCenter sharedCenter]];
    
    //customize tab bar
    UITabBarController *tabViewController = (UITabBarController *)self.window.rootViewController;
    
    UITabBar *tabBar = tabViewController.tabBar;
    tabBar.backgroundImage = [UIImage imageNamed:@"tab.png"];
    tabBar.selectionIndicatorImage = [UIImage imageNamed:@"TabDown_bg.png"];
    
     
    //customize tab bar items    
    NSArray *tabBarItems = tabBar.items;
    
    UITabBarItem *generalItem = [tabBarItems objectAtIndex:0];
    [generalItem setFinishedSelectedImage:[UIImage imageNamed:@"General_down.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"General.png"]]; 
    
    UITabBarItem *appItem = [tabBarItems objectAtIndex:1];
    [appItem setFinishedSelectedImage:[UIImage imageNamed:@"App_down.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"App.png"]];
    
//    UITabBarItem *finderItem = [tabBarItems objectAtIndex:2];
//    [finderItem setFinishedSelectedImage:[UIImage imageNamed:@"Finder_down.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Finder.png"]];
    
    UITabBarItem *dockItem = [tabBarItems objectAtIndex:2];
    [dockItem setFinishedSelectedImage:[UIImage imageNamed:@"Dock_down.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"Dock.png"]];
    
    UITabBarItem *moreItem = [tabBarItems objectAtIndex:3];
    [moreItem setFinishedSelectedImage:[UIImage imageNamed:@"More_down.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"More.png"]];
    
    
    UIColor *titleDarkColor = [UIColor whiteColor];//[UIColor colorWithRed:72.0f/255.0f green:145.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    UIColor *titleLightColor = [UIColor colorWithRed:194.0f/255.0f green:211.0f/255.0f blue:223.0f/255.0f alpha:1.0f];
    
    for (UITabBarItem *item in tabBarItems) {
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObject:titleDarkColor forKey:UITextAttributeTextColor] forState:UIControlStateSelected];
        [item setTitleTextAttributes:[NSDictionary dictionaryWithObject:titleLightColor forKey:UITextAttributeTextColor] forState:UIControlStateNormal];
    }
    
    
    //customize navigation bar
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"top.png"] 
                                       forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObject:titleLightColor forKey:UITextAttributeTextColor]];
    
    //customize tool bar
    [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"top.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    //customize bar button item
    UIImage *img = [UIImage imageNamed:@"BarButtonItem.png"];
    UIImage *stretchImg = [img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [[UIBarButtonItem appearance] setBackgroundImage:stretchImg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Saves changes in the application's managed object context before the application terminates.
    //[self saveContext];
}


@end





@implementation UIBarButtonItem (EditBarItem)

+ (UIBarButtonItem *)editBarItemWithTarget:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"Edit" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0f]];
    
    UIImage *image = [UIImage imageNamed:@"BarButtonItem.png"];
    UIImage *stretchImg = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [button setBackgroundImage: stretchImg forState:UIControlStateNormal];
    button.frame= CGRectMake(0.0, 0.0, 50, 30);
    
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    return item;
}

@end

